﻿using System;
using ELab.Communication;

namespace ELab.Client.ELabServices
{
    public class ELabServiceConsumer : IDisposable
    {
        #region Public fields

        /// <summary>
        /// Underlying ELabClient object to send/receive ELab messages.
        /// </summary>
        internal ELabClient ELabClient { get; private set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new ELabServiceApplication object with default values to connect to ELab server.
        /// </summary>
        /// <param name="applicationName">Name of the application</param>
        public ELabServiceConsumer(string applicationName)
        {
            ELabClient = new ELabClient(applicationName, CommunicationConsts.DefaultIpAddress, CommunicationConsts.DefaultPort);
            Initialize();
        }

        /// <summary>
        /// Creates a new ELabServiceApplication object with default port to connect to ELab server.
        /// </summary>
        /// <param name="applicationName">Name of the application</param>
        /// <param name="ipAddress">IP address of ELab server</param>
        public ELabServiceConsumer(string applicationName, string ipAddress)
        {
            ELabClient = new ELabClient(applicationName, ipAddress, CommunicationConsts.DefaultPort);
        }

        /// <summary>
        /// Creates a new ELabServiceApplication object.
        /// </summary>
        /// <param name="applicationName">Name of the application</param>
        /// <param name="ipAddress">IP address of ELab server</param>
        /// <param name="port">TCP port of ELab server</param>
        public ELabServiceConsumer(string applicationName, string ipAddress, int port) 
        {
            ELabClient = new ELabClient(applicationName, ipAddress, port);
        }

        #endregion

        #region Public methods

        /// <summary>
        /// This method connects to ELab server using underlying ELabClient object.
        /// </summary>
        public void Connect()
        {
            ELabClient.Connect();
        }

        /// <summary>
        /// This method disconnects from ELab server using underlying ELabClient object.
        /// </summary>
        public void Disconnect()
        {
            ELabClient.Disconnect();
        }

        /// <summary>
        /// Disposes this object, disposes/closes underlying ELabClient object.
        /// </summary>
        public void Dispose()
        {
            ELabClient.Dispose();
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Initializes this object.
        /// </summary>
        private void Initialize()
        {
            ELabClient.CommunicationWay = CommunicationWays.Send;
        }
        
        #endregion
    }
}
